//
//  AspectsViewController.m
//  DebugDemo
//
//  Created by XFB on 2018/12/14.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "AspectsViewController.h"

static CGFloat TopY = 200.0;
#define BottomY ([UIScreen mainScreen].bounds.size.height - 200)

@interface AspectsViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate>
/** <#property#> */
@property (weak, nonatomic) UITableView *tableView;

/** <#property#> */
@property (assign, nonatomic) CGFloat currentOffsetY;

/** <#property#> */
@property (weak, nonatomic) UIPanGestureRecognizer *bottomPan;

@end

@implementation AspectsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, BottomY, self.view.bounds.size.width, self.view.bounds.size.height - TopY)];
    tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    self.tableView = tableView;
    
    UIPanGestureRecognizer *pan = tableView.panGestureRecognizer;
    tableView.scrollEnabled = NO;
    [pan addTarget:self action:@selector(panGestureEvent:)];
    
    UIPanGestureRecognizer *bottomPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(bottomPanGestureEvent:)];
    bottomPan.delegate = self;
    self.bottomPan = bottomPan;
    [tableView addGestureRecognizer:bottomPan];
}

- (void)panGestureEvent:(UIPanGestureRecognizer *)pan {
    CGRect frame = self.tableView.frame;
    CGPoint translation = [pan translationInView:self.view];
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        self.currentOffsetY = self.tableView.contentOffset.y;
    }
    
    if (pan.state == UIGestureRecognizerStateChanged) {
        if (translation.y > 0 && frame.origin.y < BottomY && self.currentOffsetY <= 0) {
            [self.tableView setContentOffset:CGPointMake(0, self.currentOffsetY)];
            frame.origin.y = TopY + translation.y;
            self.tableView.frame = frame;
        }
    }
    
    if (pan.state == UIGestureRecognizerStateEnded) {
        if (translation.y > 0 && frame.origin.y < BottomY && self.tableView.contentOffset.y <= 0) { // 往下拖
            [self.tableView setContentOffset:CGPointZero];
            frame.origin.y = BottomY;
            self.bottomPan.enabled = YES;
            pan.enabled = NO;
            [UIView animateWithDuration:0.25 animations:^{
                self.tableView.frame = frame;
            } completion:^(BOOL finished) {
            }];
        }
    }
}

- (void)bottomPanGestureEvent:(UIPanGestureRecognizer *)bottomPan {
    if (!bottomPan.enabled) { return; }
    CGRect frame = self.tableView.frame;
    
    CGPoint translation = [bottomPan translationInView:bottomPan.view];
    
    if (bottomPan.state == UIGestureRecognizerStateChanged) {
        if (translation.y < 0 && frame.origin.y >= TopY && BottomY + translation.y > 200) {
            frame.origin.y = BottomY + translation.y;
            self.tableView.frame = frame;
            return;
        }
    }
    
    if (bottomPan.state == UIGestureRecognizerStateEnded && translation.y < 0) {
        NSLog(@"translation.y%f", translation.y);
        self.tableView.scrollEnabled = YES;
        frame.origin.y = TopY;
        bottomPan.enabled = NO;
        self.tableView.panGestureRecognizer.enabled = YES;
        [UIView animateWithDuration:0.25 animations:^{
            self.tableView.frame = frame;
        }];
    }
}

#pragma mark - Table view data source
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([otherGestureRecognizer.view isKindOfClass:[UITableView class]]) {
        return YES;
    }
    return NO;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    UITableViewCell *cell;
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld行", indexPath.row];
    
    // Configure the cell...
    
    return cell;
}

- (void)scrollViewDidEndDragging:(UITableView *)tableView willDecelerate:(BOOL)decelerate {
    if (decelerate) { return; }
    BOOL enabled = tableView.frame.origin.y > TopY;
    self.tableView.panGestureRecognizer.enabled = !enabled;
    self.bottomPan.enabled = enabled;
}

- (void)scrollViewDidEndDecelerating:(UITableView *)tableView {
    BOOL enabled = tableView.frame.origin.y > TopY;
    self.tableView.panGestureRecognizer.enabled = !enabled;
    self.bottomPan.enabled = enabled;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"dddddddddddddddddddddd");
    
    [UIView animateWithDuration:0.25 animations:^{
        self.tableView.y = BottomY;
    }];
}


@end

